﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace MM.Common.Base
{
    public static class Gzip
    {
        // Methods
        public static byte[] GzipCompress(string uncompressedString)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(uncompressedString);
            MemoryStream stream = new MemoryStream();
            Stream stream2 = new GZipStream(stream, CompressionMode.Compress);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.Close();
            return stream.ToArray();
        }

        public static string GzipCompress(byte[] bytdata)
        {
            MemoryStream stream = new MemoryStream();
            Stream stream2 = new GZipStream(stream, CompressionMode.Compress);
            stream2.Write(bytdata, 0, bytdata.Length);
            stream2.Close();
            byte[] inArray = stream.ToArray();
            stream.Close();
            stream2.Dispose();
            stream.Dispose();
            return Convert.ToBase64String(inArray, 0, inArray.Length);
        }

        public static string GzipDeCompress(byte[] bytInput)
        {
            StringBuilder builder = new StringBuilder();
            int num = 0;
            byte[] buffer = new byte[0x1000];
            try
            {
                Stream stream = new GZipStream(new MemoryStream(bytInput), CompressionMode.Decompress);
                while (true)
                {
                    int count = stream.Read(buffer, 0, buffer.Length);
                    if (count <= 0)
                    {
                        break;
                    }
                    num += count;
                    builder.Append(Encoding.Unicode.GetString(buffer, 0, count));
                }
                stream.Close();
            }
            catch
            {
            }
            return builder.ToString();
        }
    }
}
